<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    <style type="text/css">
        .democlass{
            color:red;
        }
    </style>
</head>
<body>

<h1>Hello World</h1>
<p id="demo">单击按钮来创建一个“类”属性值“democlass”插入到上面的H1元素。</p>
<button onclick="myFunction()">点我</button>
<script>
    function myFunction(){
        let h1=document.getElementsByTagName("H1")[0];
        let att=document.createAttribute("class");
        att.value="democlass";
        h1.setAttributeNode(att);
    }
</script>

</body>
</html>